﻿using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using WaterCloud.Code;
using SqlSugar;
using WaterCloud.DataBase;
using WaterCloud.Domain.SalaryManagement;
using WaterCloud.Domain.AttendanceManagement;
using MiniExcelLibs;
using System.IO;

namespace WaterCloud.Service.SalaryManagement
{
    /// <summary>
    /// 创 建：超级管理员
    /// 日 期：2025-05-19 10:44
    /// 描 述：年终奖服务类
    /// </summary>
    public class YearendbonusService : BaseService<YearendbonusEntity>, IDenpendency
    {
        public YearendbonusService(ISqlSugarClient context) : base(context)
        {
        }
        #region 获取数据
        public async Task<List<YearendbonusEntity>> GetList(string keyword = "")
        {
            var data = repository.IQueryable();
            if (!string.IsNullOrEmpty(keyword))
            {
                data = data.Where(a => a.F_EnCode.Contains(keyword)
                || a.F_FullName.Contains(keyword));
            }
            return await data.Where(a => a.F_DeleteMark == false).OrderBy(a => a.F_Id , OrderByType.Desc).ToListAsync();
        }

        public async Task<List<YearendbonusEntity>> GetLookList(string keyword = "")
        {
            var query = repository.IQueryable().Where(a => a.F_DeleteMark == false);
            if (!string.IsNullOrEmpty(keyword))
            {
                //此处需修改
                query = query.Where(a => a.F_EnCode.Contains(keyword)
                || a.F_FullName.Contains(keyword));
            }
             //权限过滤
             query = GetDataPrivilege("a", "", query);
             return await query.OrderBy(a => a.F_Id , OrderByType.Desc).ToListAsync();
        }

        public async Task<List<YearendbonusEntity>> GetLookList(SoulPage<YearendbonusEntity> pagination,string keyword = "",string id="")
        {
            var query = repository.IQueryable().Where(a => a.F_DeleteMark == false && a.F_CreatorUserId == currentuser.UserId);
            if (!string.IsNullOrEmpty(keyword))
            {
                query = query.Where(a => a.F_EnCode.Contains(keyword)
                || a.F_FullName.Contains(keyword));
            }
            if (!string.IsNullOrEmpty(id))
            {
                query = query.Where(a => a.F_Id == id);
            }
            //权限过滤
            query = GetDataPrivilege("a", "", query);
            return await query.ToPageListAsync(pagination);
        }

        public async Task<YearendbonusEntity> GetForm(string keyValue)
        {
            var data = await repository.FindEntity(keyValue);
            return data;
        }
        #endregion

        public async Task<YearendbonusEntity> GetLookForm(string keyValue)
        {
            var data = await repository.FindEntity(keyValue);
            data.list = await repository.Db.Queryable<YearendbonusitemEntity>().Where(a => a.F_HeadId == keyValue).ToListAsync();
            return data;
        }

        #region 提交数据
        public List<YearendbonusitemEntity> CheckFile(string fileFullName)
        {
            if (!FileHelper.IsExcel(fileFullName))
            {
                throw new Exception("文件不是有效的Excel文件!");
            }
            //文件解析
            var list = MiniExcel.Query<YearendbonusitemEntity>(fileFullName).ToList(); ;
            //删除文件
            File.Delete(fileFullName);
            foreach (var item in list)
            {
                item.F_Id = Utils.GuId();
                item.F_EnabledMark = true;
                item.F_DeleteMark = false;
               
                item.F_SortCode = 1;

                List<string> str = new List<string>();
                if (string.IsNullOrEmpty(item.F_UserNum))
                {
                    item.F_EnabledMark = false;
                    item.ErrorMsg = "工号不存在";
                    continue;
                }

                if (item.F_EnabledMark == false)
                {
                    item.ErrorMsg = string.Join(',', str.ToArray());
                }
            }
            return list;
        }
        public async Task SubmitForm(YearendbonusEntity entity, string keyValue)
        {
            if (string.IsNullOrEmpty(keyValue))
            {
                //初始值添加
                entity.F_DeleteMark = false;
                entity.Create();

            }
            else
            {
                //此处需修改
                entity.Modify(keyValue);

            }

            var dataList = new List<YearendbonusitemEntity>();
            if (entity.list == null || entity.list.Count == 0)
            {
                throw new Exception("考勤明细不能为空!");
            }
            foreach (var item in entity.list)
            {
                item.Create();

                item.F_CreatorTime = entity.F_CreatorTime;
                item.F_HeadId = entity.F_Id;

                item.F_DeleteMark = false;
                item.F_CreatorTime = DateTime.Now;

                if (string.IsNullOrEmpty(item.F_UserNum))
                {
                    throw new Exception("请输入工号");
                }

                dataList.Add(item);
                entity.list = dataList;
            }
            repository.Db.Ado.BeginTran();
            if (string.IsNullOrEmpty(keyValue))
            {
                //初始值添加

                await repository.Insert(entity);
            }
            else
            {
                //此处需修改

                await repository.Update(entity);
            }
            await repository.Db.Deleteable<YearendbonusitemEntity>().Where(a => a.F_HeadId == entity.F_Id).ExecuteCommandAsync();

            //插入记录
            await repository.Db.Insertable(dataList).ExecuteCommandAsync();
            repository.Db.Ado.CommitTran();
        }

        public async Task DeleteForm(string keyValue)
        {
            var ids = keyValue.Split(',');

            //删除表体
            await repository.Db.Deleteable<YearendbonusitemEntity>().Where(a => ids.Contains(a.F_HeadId)).ExecuteCommandAsync();
            //删除表头
            await repository.Delete(a => ids.Contains(a.F_Id.ToString()));
        }
        public async Task ReviewForm(string keyValue, bool b)
        {

            var ids = keyValue.Split(',');

            await repository.Update(a => ids.Contains(a.F_Id), a => new YearendbonusEntity
            {
                F_EnabledMark = b,

            });

            await repository.Db.Updateable<YearendbonusitemEntity>().SetColumns(p => new YearendbonusitemEntity()
            {
                F_EnabledMark = b,



            }).Where(a => ids.Contains(a.F_HeadId)).ExecuteCommandAsync();
        }


        #endregion

    }
}
